package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.FindAppItem2Bean;

import java.util.List;

/**
 * Created by Jogger on 2017/1/20.
 * 发现应用--美友--用户推荐
 */

public class UserRecommandAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<FindAppItem2Bean> mAppList;
    private int mHeaderCount = 1;//头部View个数
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(View view, FindAppItem2Bean app);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public UserRecommandAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }
        if (position == 1) {
            return 1;
        }
        return 2;
    }

    public int getHeaderCount() {
        return mHeaderCount;
    }

    //判断当前item是否是HeadView
    public boolean isHeaderView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int
            viewType) {
        View view;
        if (viewType == 0) {
            view = LayoutInflater.from(mContext).inflate(R.layout.rv_user_recommand_item_title,
                    parent, false);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.rv_user_recommand_item, parent,
                    false);
        }
        return new UserRecommandAdapter.MyItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final FindAppItem2Bean app = mAppList.get(position);
//        if (holder instanceof MyHeadViewHolder) {
//            MyHeadViewHolder headHolder = (MyHeadViewHolder) holder;
//            Picasso.with(mContext)
//                    .load(mUser.getAvatar_url())
//                    .config(Bitmap.Config.RGB_565)
//                    .into(headHolder.iv_avatar);
//            headHolder.tv_career.setText(mUser.getCareer());
//            headHolder.tv_user_name.setText(mUser.getName());
//            headHolder.tv_recommand_count.setText(mRecommandCount+"");
//        } else {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
        if (getItemViewType(position) == 0) {
            itemHolder.tv_flowers_sum.setText(app.getAuthor_flowers() + "");
        } else {
            itemHolder.tv_title.setText(app.getTitle());
            itemHolder.tv_sub_title.setText(app.getSub_title());
            Picasso.with(mContext)
                    .load(app.getIcon_image())
                    .config(Bitmap.Config.RGB_565)
                    .into(itemHolder.iv_head);
            Picasso.with(mContext)
                    .load(app.getCover_image())
                    .config(Bitmap.Config.RGB_565)
                    .into(itemHolder.iv_cover_image);
            itemHolder.tv_up_times.setText(app.getUp_times() + "");
            itemHolder.down_times.setText(app.getDown_times() + "");
            itemHolder.tv_comment_times.setText(app.getComments().size() + "");
            itemHolder.ll_content_container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onItemClick(view, app);
                    }
                }
            });
        }
//        }
    }

//    public void loadHeadData(FindAppItem5Bean.UserBean userBean, int recommandCount) {
//        mUser = userBean;
//        mRecommandCount = recommandCount;
//        notifyDataSetChanged();
//    }

    public void loadData(List<FindAppItem2Bean> appList) {
        mAppList = appList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mAppList.size();
    }

    public class MyHeadViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_head_container;
        LinearLayout ll_head_container;
        ImageView iv_avatar;
        TextView tv_user_name;
        TextView tv_career;
        TextView tv_recommand_count;
        TextView tv_collect_count;

        public MyHeadViewHolder(View itemView) {
            super(itemView);
            rl_head_container = (RelativeLayout) itemView.findViewById(R.id.rl_head_container);
            ll_head_container = (LinearLayout) itemView.findViewById(R.id.ll_head_container);
            iv_avatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tv_user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
            tv_career = (TextView) itemView.findViewById(R.id.tv_career);
            tv_recommand_count = (TextView) itemView.findViewById(R.id.tv_recommand_count);
            tv_collect_count = (TextView) itemView.findViewById(R.id.tv_collect_count);

        }
    }

    class MyItemViewHolder extends RecyclerView.ViewHolder {
        TextView tv_flowers_sum;
        TextView tv_title;
        ImageView iv_head;
        TextView tv_sub_title;
        ImageView iv_cover_image;
        TextView tv_up_times;
        TextView down_times;
        TextView tv_comment_times;
        LinearLayout ll_content_container;

        MyItemViewHolder(View itemView) {
            super(itemView);
            ll_content_container = (LinearLayout) itemView.findViewById(R.id.ll_content_container);
            tv_flowers_sum = (TextView) itemView.findViewById(R.id.tv_flowers_sum);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_sub_title = (TextView) itemView.findViewById(R.id.tv_sub_title);
            iv_cover_image = (ImageView) itemView.findViewById(R.id.iv_cover_image);
            tv_up_times = (TextView) itemView.findViewById(R.id.tv_up_times);
            down_times = (TextView) itemView.findViewById(R.id.down_times);
            tv_comment_times = (TextView) itemView.findViewById(R.id.tv_comment_times);
        }
    }
}
